package com.tz.crm.controller.system;

import com.tz.crm.entity.Announcement;
import com.tz.crm.service.AnnouncementService;
import com.tz.crm.utils.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by 钟 on 2017/3/14.
 */
@Controller
@RequestMapping("system/announcement")
public class AnnouncementController {
    @Inject
    private AnnouncementService announcementService;
    @RequestMapping("/search.action")
    public String list(HttpServletRequest request){
        Pager initPager=new Pager();
        String pageNumber=request.getParameter("pager.pageNumber");
        if(null!=pageNumber){
            initPager.setPageNumber(Integer.parseInt(pageNumber));
        }
        Pager pager=announcementService.getByPager(initPager,new Announcement());
        request.setAttribute("pager",pager);
        return "system/announce_manage";
    }
    @RequestMapping("/delete.action")
    public String delete(HttpServletRequest request){
        String id=request.getParameter("ac.id");
        announcementService.delete(id);
        Pager pager=announcementService.getByPager(new Pager(),new Announcement());
        request.setAttribute("Annpager",pager);
        return "redirect:search.action";
    }
    @RequestMapping("/initUpdate.action")
    public String initUpdate(HttpServletRequest request){
        String id=request.getParameter("ac.id");
        Announcement announcement=announcementService.get(id);
        request.setAttribute("announcement",announcement);
        return "system/announce_modify";
    }
    @RequestMapping("/Update.action")
    public String update(HttpServletRequest request){
        String id=request.getParameter("ac.id");
        String title=request.getParameter("ac.title");
        String context=request.getParameter("ac.context");
        //获取被修改的announcementId
        Announcement announcement=announcementService.get(id);
        announcement.setTitle(title);
        announcement.setContext(context);
        announcementService.update(announcement);
        return "redirect:search.action";
    }
    @RequestMapping("/initAdd.action")
    public String initAdd(){
        return "system/announce_add";
    }
    @RequestMapping("/add.action")
    public String add(HttpServletRequest request){
        String title=request.getParameter("ac.title");
        String context=request.getParameter("ac.context");
        Announcement announcement=new Announcement();
        announcement.setTitle(title);
        announcement.setContext(context);
        announcementService.save(announcement);
        return "redirect:search.action";
    }
}
